<template>
  <div class="container">
    <div class="header">
      <el-button type="primary" plain size="small" @click="dialogFormVisible=true">新增</el-button>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="role_zh"
          label="角色名称">
        </el-table-column>
        <el-table-column
          prop="role_en"
          label="角色标识">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="paginator">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10, 20, 30, 40]"
        :page-size=size
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
      </el-pagination>
    </div>
    <!--    弹窗-->
    <div>
      <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
        <el-form :model="form" :rules="rules" ref="formData">
          <el-form-item label="角色名" :label-width="formLabelWidth" prop="role_zh">
            <el-input v-model="form.role_zh" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色标识" :label-width="formLabelWidth" prop="role_en">
            <el-input v-model="form.role_en" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="描述" :label-width="formLabelWidth">
            <el-input v-model="form.description" type="textarea" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>


        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import {addRole, deleteRole, editRole, getOneRole, rolePage} from "@/api/role";
import {title} from "@/settings";

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      dialogFormVisible: false,
      formLabelWidth: "80px",
      currentPage: 1,
      size: 5,
      total: 0,
      tableData: [],
      form: {
        'role_zh': "",
        "role_en": "",
        "description": ""
      },
      rules: {
        role_zh: [
          {required: true, message: '请输入角色名称', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        role_en: [
          {required: true, message: '请输入角色标识', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ]

      }
    }
  },
  methods: {
    clearInfo() {
      this.form = {
        'role_zh': "",
        "role_en": "",
        "description": ""
      }
      // 清空校验规则
      this.$refs.formData.clearValidate()
      this.title = "新增"
    },
    submitForm() {
      this.$refs.formData.validate(val => {
        if (val) {
          if (this.form.id) {
            editRole(this.form).then(response => {
              this.$notify.success(response.message)
              this.dialogFormVisible = false
              this.tableDataPage()
            })
          } else {
            addRole(this.form).then(response => {
              this.$notify.success(response.message)
              this.dialogFormVisible = false
              this.tableDataPage()
            })
          }

        } else {
          this.$notify.warning('请填写完整的表单数据')
        }
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.tableDataPage()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.tableDataPage()
    },
    handleEdit(index, row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      // 发送异步请求 获取角色信息
      getOneRole(row.id).then(response => {
        this.form = response.role
      })
    },
    handleDelete(index, row) {
      deleteRole(row.id).then(response=>{
           this.$notify.success(response.message)
           this.tableDataPage()
      })
    },
    tableDataPage() {
      rolePage({page: this.currentPage, size: this.size}).then(response => {
        this.total = response.total
        this.tableData = response.table_data
      })
    }
  },
  created() {
    this.tableDataPage()
  }
}
</script>


<style scoped lang="scss">
.container {
  padding: 20px;
  box-sizing: border-box;
}

.header {
  margin-top: 50px;
  margin-bottom: 20px;
}

.paginator {
  margin-top: 15px;
}
</style>
